// pages/search/search.js
const app = getApp();
var util = require("../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "", //当前歌星的名称
    searchValue: "", //input输入的内容
    hotShow: true, //热门搜索显示
    shishiShow: false, //实时搜索显示
    resultShow: false, //搜索结果显示
    shishiData: [], //实时搜索结果
    songResultData: [], //歌曲搜索结果
    hotData: [], //热门搜索
  },
    // 播放歌曲
    play(e) {
      var that = this;
      console.log(e.currentTarget.dataset.code);
      var token = wx.getStorageSync("token");
      var code = e.currentTarget.dataset.code;
      var tempParam = {
        code: code,
      };
      if (token) {
        util.postToken("/wechat/play", tempParam, token, function (res) {
          console.log(res.status_code, "播放");
          if (res.status_code == 200) {
            wx.showToast({
              title: "成功",
              icon: "success",
              duration: 500,
            });
          } else {
          }
        });
      }
    },
    // 添加歌曲
    add(e) {
      var that = this;
      var codes = e.currentTarget.dataset.code;
      // "fa15884d-0ebb-388e-b179-363a077e089f"
      var uid = app.globalData.uid;
      // var uid = wx.getStorageSync("uid");
      var token = wx.getStorageSync("token");
      var headurl = wx.getStorageSync("wxImg");
      // console.log("avatarUrl:", wx.getStorageSync("avatarUrl"));
      // var headeimg = wx.getStorageSync("avatarUrl");
      var tempParam = {
        codes: codes,
        uid: uid,
        headurl: headurl,
      };
      if (token) {
        util.postToken("/wechat/playlist/song/add", tempParam, token, function (
          res
        ) {
          console.log(res);
        });
      } else {
      }
    },
    // 收藏歌曲
    love(e) {
      var that = this;
      var songCode = e.currentTarget.dataset.code;
      var token = wx.getStorageSync("token");
      var tempParam = {
        songCode: songCode,
      };
      if (token) {
        util.postToken("/wechat/song/favorites/add", tempParam, token, function (
          res
        ) {
          console.log(res, "收藏");
        });
      }
    },
  // input输入时
  input: function (e) {
    var that = this;
    if (e.detail.value == "") {
      that.setData({
        searchValue: "",
        hotShow: true,

        shishiShow: false,
        resultShow: false,
      });
    } else {
      that.setData({
        searchValue: e.detail.value,
        hotShow: false,

        shishiShow: true,
        resultShow: false,
      });
      // console.log(e.detail.value)
      // 实时搜索

      let params = {
        filter: [
          {
            matchType: "like",
            matchName: "singer",
            matchValue: that.data.name,
          },
          {
            matchType: "like",
            matchName: "name",
            matchValue: that.data.searchValue,
          },
        ],
        per_page: 10,
        page: 1,
      };
      wx.request({
        url: util.api + "/songs",
        method: "POST",
        data: JSON.stringify(params),
        success(res) {
          if (res.statusCode == 200) {
            console.log(res.data.data, "000");
            that.setData({
              shishiData: res.data.data.data,
            });
          }
        },
      });
    }
  },
  onSearch(e) {
    var that = this;
    that.setData({
      hotShow: false,
      shishiShow: false,
      resultShow: true,
    });
    let params = {
      filter: [
        { matchType: "like", matchName: "singer", matchValue: that.data.name },
        { matchType: "like", matchName: "name", matchValue: e },
      ],
      per_page: 10,
      page: 1,
    };
    wx.request({
      url: util.api + "/songs",
      method: "POST",
      data: JSON.stringify(params),
      success(res) {
        if (res.statusCode == 200) {
          console.log(res.data.data, "000");
          that.setData({
            songResultData: res.data.data.data,
          });
        }
      },
    });
  },
  // input确认输入时
  // from,有from代表有传入的值
  inputConfirm: function (e) {
    var that = this;
    that.setData({
      searchValue: e.detail.value,
    });
    that.onSearch(that.data.searchValue);
  },
  // 实时搜索结果点击
  shishiClick(e) {
    this.setData({
      searchValue: e.currentTarget.dataset.name,
    });

    this.onSearch(e.currentTarget.dataset.name, "shishi");
  },
  // 热门搜索点击
  clickHot(e) {
    this.setData({
      searchValue: e.currentTarget.dataset.name,
    });
    this.onSearch(e.currentTarget.dataset.name);
  },
  // 取消搜索
  cancelSearch() {
    var that = this;
    that.setData({
      searchValue: "",
      hotShow: true,

      shishiShow: false,
      resultShow: false,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options.name);
    that.setData({
      name: options.name,
    });
    let params = {
      filter: [
        { matchType: "like", matchName: "singer", matchValue: that.data.name },
      ],
      per_page: 6,
      page: 1,
    };
    wx.request({
      url: util.api + "/songs",
      method: "POST",
      data: JSON.stringify(params),
      success(res) {
        if (res.statusCode == 200) {
          console.log(res.data.data, "000");
          that.setData({
            hotData: res.data.data.data,
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //重新获取缓存
    var that = this;
    // that.reflash();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
